#include "admin.h"

// 用于 for_each 的遍历操作
void print_book(book temp)
{
    cout << "书名:" << temp.get_book_name();
    cout << "\t作者:" << temp.get_author();
    cout << "\t价格:" << temp.get_price();
    string str1 = "借出";
    string str2 = "在馆";
    cout << temp.get_is_sta() ? str1 : str2 ;

    cout << endl;
}

//构造函数
admin::admin()
        : book_v(MAX_BOOK_NUM)
{
}

//析构函数
admin::~admin()
{
}

//增加书籍
void admin::add_book(const book &new_book)
{
    book_v.push_back(new_book);
}

//删除书籍
void admin ::delete_book(const string &book_name)
{
    this->book_name = book_name;

    //查找
    book temp_book;
    temp_book.set_book_name(book_name);
    vector<book>::iterator it = find(this->book_v.begin() , this->book_v.end() , temp_book);

    //cout << it->get_author() << endl;
    //删除
    book_v.erase(it);
}

//修改书籍信息
void admin::modify_book(const string &book_name, const book &updated_book)
{
    //查找
    book temp_book;
    temp_book.set_book_name(book_name);
    vector<book>::iterator it = find(this->book_v.begin() , this->book_v.end() , temp_book);

    it->set_book_name(updated_book.get_book_name());
    it->set_author(updated_book.get_author());
    it->set_price(updated_book.get_price());
    it->set_is_sta(updated_book.get_is_sta());
}

//查询书籍
book admin::search_book(const string &book_name)
{
    // 查找
    book temp_book;
    temp_book.set_book_name(book_name);
    vector<book>::iterator it = find(this->book_v.begin(), this->book_v.end() , temp_book );
    
    return *it;
}

// 遍历
void admin::view_all_books() const
{
    for_each(this->book_v.begin() , this->book_v.end() , print_book);
}

bool admin::find_book_name(book book_temp)
{
    return book_temp.get_book_name() == this->book_name;
}
